<template>
  <div id="app">
    <div>
      <span>姓名:</span>
      <input type="text" v-model="name"/>
    </div>
    <div>
      <span>年龄:</span>
      <input type="number" v-model="age"/>
    </div>
    <div>
      <span>性别:</span>
      <select v-model="gender">
        <option value="男">男</option>
        <option value="女">女</option>
      </select>
    </div>
    <div>
      <button @click="add">添加/修改</button>
    </div>
    <div>
      <table
        border="1"
        cellpadding="10"
        cellspacing="0"
      >
        <tr>
          <th>序号</th>
          <th>姓名</th>
          <th>年龄</th>
          <th>性别</th>
          <th>操作</th>
        </tr>
        <tr v-for="(item,index) in arr" :key="index+1">
          <td>{{index+1}}</td>
          <td>{{item.name}}</td>
          <td>{{item.age}}</td>
          <td>{{item.gender}}</td>
          <td>
            <button @click="del(index)">删除</button>
            <button @click="edit(index)">编辑</button>
          </td>
        </tr>
      </table>
    </div>
  </div>
</template>
<script>
export default {
  data(){
      return {
          arr:[
              {name:'小明',age:18,gender:'男'},
              {name:'韩梅梅',age:15,gender:'女'},
              {name:'张三',age:12,gender:'男'},
              {name:'小花',age:20,gender:'女'},
          ],
          name:'',
          age:'',
          gender:'',
          isEdit:''
      }
  },
  methods:{
    //   添加
      add(){
          //非空校验
          if(!this.name||!this.age||!this.gender){
              return alert('内容不能为空哦')
          }
          const obj={
              name:this.name,
              age:this.age,
              gender:this.gender
          }
          this.arr.push(obj)
          //清空内容
          this.name=this.age=this.gender=''
      },
    //   编辑
      edit(i){
          this.name=this.arr[i].name
          this.age=this.arr[i].age
          this.gender=this.arr[i].gender
          //点完编辑之后需要删除点完的这一行
        //   this.arr.splice(i,1)
      },
    //   删除
    del(i){
        this.arr.splice(i,1)
    },
    // 修改

  }
}
</script>
